package com.uchedao.vehicledetection.view.carlist.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import com.uchedao.vehicledetection.R;
import com.uchedao.vehicledetection.view.carlist.Entity.CarModel;
import java.util.ArrayList;
import java.util.HashMap;

/**
 * Created by ZhangZhanghua on 2015/8/26 0026.
 */
public class ModelAdapter extends BaseAdapter {

    private Context mContext;
    private LayoutInflater mInflater;
    private ArrayList<CarModel> modelList;
    private String[] sections;
    HashMap<String, Integer> map = new HashMap<String, Integer>();

    public ModelAdapter(Context context , ArrayList<CarModel> list){
        mContext = context;
        mInflater = LayoutInflater.from(context);
        modelList = list;
        sections = (String[]) getSections();
    }
    @Override
    public int getCount() {
        return modelList.size();
    }

    @Override
    public Object getItem(int position) {
        return modelList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder = null;
        if (convertView ==null){
            convertView = mInflater.inflate(R.layout.list_item, null);
            viewHolder = new ViewHolder();
            viewHolder.tvLetter = (TextView) convertView.findViewById(R.id.catalog);
            viewHolder.tvTitle = (TextView) convertView.findViewById(R.id.title);
            convertView.setTag(viewHolder);
        }else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
//            viewHolder.tvLetter.setVisibility(View.GONE);
//        viewHolder.tvTitle.setText(modelList.get(position).getName());
//        return convertView;
        int section = getSectionForPosition(position);

        if(position == getPositionForSection(section)){
//            if (modelList.get(position).getModelYear().equals("-1")){
//                viewHolder.tvLetter.setVisibility(View.GONE);
//            }
            viewHolder.tvLetter.setVisibility(View.VISIBLE);
            viewHolder.tvLetter.setText((modelList.get(position).getModelYear()+ ""));
        }else{
            viewHolder.tvLetter.setVisibility(View.GONE);
        }

        viewHolder.tvTitle.setText(modelList.get(position).getName());
        return convertView;
    }

    //    public String getSectionForPosition(int position) {
//        return modelList.get(position).getModelYear();
//    }
//
//    public int getPositionForSection(String section) {
//        for (int i = 0; i < getCount(); i++) {
//            String sortStr = modelList.get(i).getModelYear();
//            if (sortStr.equals(section)) {
//                return i;
//            }
//        }
//
//        return -1;
//    }
    public Object[] getSections() {
        if (sections == null) {
            ArrayList<String> array = new ArrayList<String>();
            String s = "";
            for (int i = 0; i < modelList.size(); i++) {
                s = modelList.get(i).getIndexSelectAlpha();
                if (!array.contains(s)) {
                    array.add(s);
                    map.put(s, i);
                }
            }
            sections = array.toArray(new String[0]);
        }
        return sections;
    }

    public int getPositionForSection(int sectionIndex) {
        String key = sections[sectionIndex].toString();
        if (map.containsKey(key))
            return map.get(key);
        else
            return 0;
    }

    public int getSectionForPosition(int position) {
        String key = modelList.get(position).getIndexSelectAlpha();

        for (int i = 0; i < sections.length; i++) {
            if (key.equals(sections[i]))
                return i;
        }

        return 0;
    }
    private final class ViewHolder
    {
        TextView tvLetter;
        TextView tvTitle;
    }
}
